package com.ewing.busi.customer.action;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.ewing.busi.customer.dto.LightCustomerResp;
import com.ewing.busi.customer.model.Customer;
import com.ewing.busi.customer.service.CustomerService;
import com.ewing.common.utils.SystemPropertyUtils;
import com.ewing.core.app.action.base.BaseAction;
import com.ewing.utils.IntegerUtils;

/**
 * 客户信息相关的操作
 * 
 * @author Joeson Chan<chenxuegui1234@163.com>
 * @since 2016年2月3日
 * 
 */
public class CustomerAction extends BaseAction {

  /**
   * 
   * created by Joeson on 2016年4月24日 下午4:43:59
   */
  private static final long serialVersionUID = 1L;

  private static Logger logger = Logger.getLogger(CustomerAddressAction.class);

  @Resource
  private CustomerService customerService;

  /**
   * 根据id查找客户信息
   * 
   * @author Joeson
   */
  // @CustomerLoginFilter
  public void queryById() {
    try {
      LightCustomerResp customer =
          LightCustomerResp.parseCustomer(customerService.queryById(getLoginCusId()));
      outSucResult(customer);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      outFailResult("内部异常");
    }
  }

  /**
   * 保存客户信息
   * 
   * @author Joeson
   */
  public void saveCustomer() {
    try {
      Customer customer = getParamJson(Customer.class);

      customerService.add(customer);
      outSucResult(customer);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      outFailResult("内部异常");
    }
  }

  /**
   * 更新客户信息
   * 
   * @author Joeson
   */
  // @CustomerLoginFilter
  public void updateCustomer() {
    try {
      Customer customer = getParamJson(Customer.class);
      isTrue(IntegerUtils.equals(customer.getId(),
          SystemPropertyUtils.needAuth() ? getLoginCusId() : customer.getId()), "非法操作");
      customerService.update(customer);
      outSucResult(customer);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      outFailResult("内部异常");
    }
  }

}
